#include <QApplication>
#include <QDebug>
#include <QtWebKitWidgets/QWebView>
#include <QtWebKitWidgets/QWebFrame>

class QFoo : public QObject
{
    Q_OBJECT
public:
    QFoo(QObject *parent) {}
    void initialize(QWebView *wv);

public slots:
    QString print(const QString &msg);

private slots:
    void addObject();

private:
    QWebFrame *wf;
};

#include "moc.h"

void QFoo::initialize(QWebView *wv)
{
    wf = wv->page()->mainFrame();
  // Add to the javascript window object
    addObject();
    // connect the signal for javascript window object cleared, which happened
    // at page switching etc.
    connect(wf, SIGNAL(javaScriptWindowObjectCleared()), this, SLOT(addObject()));
}

void QFoo::addObject()
{
    wf->addToJavaScriptWindowObject(QString("QFoo"), this);
}

QString QFoo::print(const QString &msg)
{
    //qDebug() << "Output the message " << msg;
    return msg;
}

int main(int argc, char *argv[])
{
    QApplication test(argc, argv);

    QWebView *wv = new QWebView();

    QFoo *foo = new QFoo(wv);
    foo->initialize(wv);

    wv->setUrl(QUrl("file:///home/douzy/foo.html"));
    wv->show();

    return test.exec();
}
